﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using TIMETABLEASY.iCalManager;

using TIMETABLEASY.EasyCalendar.Model;
using TIMETABLEASY.EasyCalendar.Controler;

using TIMETABLEASY.TokenAuthentification;

namespace TIMETABLEASY
{
    public partial class Window1
    {
        #region fonctions rooms
        ////////////////////////FONCTIONS ROOMS////////////////////////////////


        //Listing des salles enregistrés
        private void list_rooms_offline(ListBox maListe)
        {
            maListe.Items.Clear();

            foreach (Rooms s in (from e in db_local.Rooms select e))
            {
                maListe.Items.Add(s.Name);

            }
        }


        //Affichage de la liste des salles
        private void display_rooms_offline(String rooms)
        {
            foreach (Rooms s in (from e in db_local.Rooms select e))
            {
                if (s.Name == rooms)
                {
                    room_name_textbox.Text = s.Name;

                    room_campus_combobox.Items.Clear();
                    chargement_liste_campus_room_offline();
                    room_campus_combobox.SelectedValue = s.Campus;

                    if (s.Available == 1)
                    {
                        room_available_label.Content = "Available";
                    }

                    else
                    {
                        room_available_label.Content = "Unavailable";
                    }

                }

            }
        }


        //Rafraichissement des éléments
        private void refresh_rooms_offline()
        {
            room_name_textbox.Text = "";
            room_available_label.Content = "";
            list_rooms(room_listbox);
            chargement_liste_campus_room_offline();
        }


        //Ajout d'une salle
        private void add_rooms_offline()
        {
            Rooms room1 = new Rooms();

            room1.Name = room_name_textbox.Text.ToLower();
            room1.Campus = room_campus_combobox.SelectedItem.ToString().ToLower();
            room1.Available = 1;

            db_local.Rooms.InsertOnSubmit(room1);
            db_local.SubmitChanges();

            //Rafraichissement des éléments
            refresh_rooms_offline();


        }


        //Mise à jour d'une salle
        private void update_rooms_offline()
        {
            if (room_name_textbox.Text != "")
            {
                Rooms room2 = (from e in db_local.Rooms
                               where e.Name == (string)room_listbox.SelectedValue
                               select e).FirstOrDefault();

                room2.Name = room_name_textbox.Text.ToLower();
                room2.Campus = room_campus_combobox.SelectedItem.ToString().ToLower();

                db_local.SubmitChanges();

                //Rafraichissement des éléments
                refresh_rooms_offline();
            }

        }


        //Suppression d'un student
        private bool delete_rooms_offline()
        {
            if (room_name_textbox.Text != "")
            {
                Rooms room3 = (from e in db_local.Rooms
                               where e.Name == (string)room_listbox.SelectedValue
                               select e).FirstOrDefault();

                if (room3.Available == 1)
                {
                    db_local.Rooms.DeleteOnSubmit(room3);
                    db_local.SubmitChanges();
                    //Rafraichissement des éléments
                    refresh_rooms_offline();
                    return true;
                }

                else
                {
                    MessageBox.Show("The room you want to delete is taken. Please delete the related event first");
                    return false;
                }

                //Rafraichissement des éléments


            }

            return true;

        }

        private void chargement_liste_campus_room_offline()
        {
            room_campus_combobox.Items.Clear();

            foreach (Campus s in (from e in db_local.Campus select e))
            {
                room_campus_combobox.Items.Add(s.Name);
            }
        }


        #endregion
    }
}
